<template>
  <div class="user-container">
    <div v-show="$route.path == '/user'">
      <div>
        <van-row class="userinfo">
          <van-col span="6">
            <img :src="getUserInfo.avatar" alt="" />
          </van-col>
          <van-col span="15">
            <span class="name" @click="showPopup">{{
              getUserInfo.nickname
            }}</span>
          </van-col>
          <van-col span="3">
            <van-icon name="arrow" size="34px" color="#fff" />
          </van-col>
        </van-row>
      </div>
      <div class="cell-groud">
        <van-cell title="笔记" is-link icon="orders-o" @click="clickFn" />
        <van-cell title="账号安全" is-link icon="shield-o" @click="clickFn" />
        <van-cell title="联系客服" is-link icon="service-o" @click="clickFn" />
        <van-cell title="帮助中心" is-link icon="warn-o" @click="clickFn" />
        <van-cell title="意见反馈" is-link icon="envelop-o" @click="clickFn" />
      </div>
      <div class="logout" @click="logout">退出登录</div>
      <compTabbar></compTabbar>
    </div>
    <transition
      v-show="$route.path == '/user/login'"
      leave-active-class="animate__animated animate__slideOutRight"
    >
      <router-view />
    </transition>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import compTabbar from "@/components/compTabbar.vue";
export default {
  components: {
    compTabbar,
  },
  data() {
    return {};
  },
  computed: {
    ...mapGetters(["getUserInfo"]),
    ...mapGetters(["getToken"]),
  },
  methods: {
    //退出
    logout() {
      localStorage.removeItem("userInfo");
      localStorage.removeItem("token");
      this.$store.commit("removeToken");
      this.$store.commit("removeUserInfo");
      this.$forceUpdate();
    },
    clickFn() {
      this.$toast("该功能暂未开放！");
    },
    showPopup() {
      if (this.getToken) {
        this.$router.push({ path: "/user" });
      } else {
        this.$router.push({ path: "/user/login" });
      }
    },
  },
  activated() {
    let token = localStorage.getItem("token");
    let userInfo = JSON.parse(localStorage.getItem("userInfo"));
    if (userInfo) {
      this.$store.commit("setToken", token);
      this.$store.commit("saveUserInfo", userInfo);
    }
  },
};
</script>

<style lang="scss" scoped>
.van-row.userinfo {
  padding: 20px 10px;
  background-color: #404040;
}
.van-row.userinfo .van-col {
  line-height: 90px;
  font-size: 18px;
  text-align: left;
  img {
    width: 90%;
    border-radius: 50%;
  }
  span.name {
    margin-left: 10px;
    color: #fff;
  }
  &:last-child {
    text-align: right;
  }
}
.cell-groud {
  margin-top: 14px;
  text-align: left;
}
.login-modal {
  width: 80vw;
  height: 30vh;
}
.van-form {
  padding: 10px;
  box-sizing: border-box;
}
.logout {
  margin: 30px auto;
  width: 80%;
  height: 50px;
  line-height: 50px;
  font-size: 18px;
  color: #fff;
  border-radius: 10px;
  background-color: #404040;
}
</style>